from pygame import *
from config_reader import *
from tool import *


class Write:
    def __init__(self):
        init()
        self.screen = display.set_mode((650, 500))
        display.set_caption("默写器")
        self.word = reader().word.get("write")
        self.screen.fill("lightskyblue")
        self.aks_font = font.SysFont("MicrosoftYaHei",25,False)
        self.ans_font = font.SysFont("MicrosoftYaHei", 25, False)
        self.button_font = font.SysFont("MicrosoftYaHei", 25, False)
        self.write_type = reader().write_language
        self.aks = reader().aks
        self.ans = ""
        self.user_ans_all = []
        self.aks_pos = 0
        self.contant = []


    def main(self):
        self.save_next = self.button_font.render(self.word.get("save_next"),False,"black","pink")
        self.save_end = self.button_font.render(self.word.get("endwrite"), False,"black", "pink")
        self.enter_band = image.load("./resources/image/enter_band_1.png").convert_alpha()
        self.enter_pos = self.aks_font.render("|",True,"red","pink")


        def pain():
            self.enter_text_TS = ""

            self.screen.fill("lightskyblue")

            if(self.write_type == "en"):
                for i in self.contant:
                    self.enter_text_TS += i
                self.ans = self.enter_text_TS
                self.enter_text_TS = ""
            elif(self.write_type == "jp"):
                for i in self.contant:
                    if(i in Tool().jp or i == " "):
                        continue
                    self.enter_text_TS += i
                    if(Tool().check_en_to_jp(self.enter_text_TS)):
                        for j in self.enter_text_TS:
                            self.contant.remove(j)
                        self.contant.append(Tool().en_to_jp(self.enter_text_TS))
                        self.enter_text_TS = ""
                self.enter_text_TS = ""
                for i in self.contant:
                    self.enter_text_TS += i
                self.ans = self.enter_text_TS
                self.enter_text_TS = ""


            self.aks_text = self.aks_font.render(self.aks[self.aks_pos],False,"black")
            self.ans_text = self.aks_font.render(self.ans, False, "black")

            self.screen.blit(self.aks_text,(20,20))
            self.screen.blit(self.enter_band, (15, 80))
            self.screen.blit(self.enter_pos,(20+self.ans_text.get_width()+3,80))
            self.screen.blit(self.ans_text, (20, 80))
            self.screen.blit(self.save_end,(15,430))
            self.screen.blit(self.save_next, (500, 430))

            pass

        while True:
            for eve in event.get():
                if (eve.type == MOUSEBUTTONDOWN):
                    print("[system]:pos:{0}".format(eve.pos))

                    if(Tool().button(self.save_next,(500,430),eve.pos)):
                        if(self.aks_pos < len(self.aks)-1):
                            self.aks_pos += 1
                            self.user_ans_all.append(self.ans)
                            self.contant = []
                            print("[system]:now wirted:{0}".format(self.user_ans_all))
                        else:
                            self.user_ans_all.append(self.ans)
                            Tool().change_cfg("./config.cfg","write","user_ans",Tool().pass_cfg(self.user_ans_all))
                            return "check"

                    if (Tool().button(self.save_end, (15, 430), eve.pos)):
                        self.user_ans_all.append(self.ans)
                        Tool().change_cfg("./config.cfg", "write", "user_ans", Tool().pass_cfg(self.user_ans_all))
                        return "check"

                if (eve.type == KEYDOWN):
                    print("[system]:key down[{0}]".format(key.name(eve.key)))
                    if (key.name(eve.key) in Tool().key_str or key.name(eve.key) in Tool().keyint_str):
                        self.contant.append(key.name(eve.key))
                    elif(key.name(eve.key) == "space"):
                        self.contant.append(" ")


                    if(key.name(eve.key) == "return"):
                        if (self.aks_pos < len(self.aks) - 1):
                            self.aks_pos += 1
                            self.user_ans_all.append(self.ans)
                            self.contant = []
                            print("[system]:now wirted:{0}".format(self.user_ans_all))
                        else:
                            self.user_ans_all.append(self.ans)
                            Tool().change_cfg("./config.cfg", "write", "user_ans", Tool().pass_cfg(self.user_ans_all))
                            return "check"


                    if (key.name(eve.key) == "backspace"):
                        self.contant.remove(self.contant[len(self.contant) - 1])

                    print("[system]:enterband contant: {0}".format(self.contant))

            pain()
            display.update()